const db = require('../db/index')
// 获取订单
exports.orderList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let sql = `select * from commodity where appid=? and userId=${pageData.userId}  limit ${from},${from + size}`
  if (pageData.storeId) {
    sql = `select * from commodity where appid=? and storeId=${pageData.storeId}  limit ${from},${from + size}`
  }
  db.query(sql, [pageData.appid], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      res: res2,
    })
  })
}
// 申请退款
exports.orderRefund = (req, res) => {
  let data = req.body
  data.reasonImg = JSON.stringify(data.reasonImg)
  let sql = `select * from commodity where _id`
  db.query(sql, [data._id], (err, res2) => {
    if (err) return res.cc(err)
    if (res2.length > 0) {
      if (data.refundCount <= res2[0].count) {
        data.status = 3
        delete data._id
        let setSql = `update commodity set ? where _id`
        db.query(setSql, data, (err, res3) => {
          res.send({
            message: '申请成功'
          })
        })
      }
    } else {
      res.send({
        message: '数量大于购买数量'
      })
    }
  })
}
// 购买订单
exports.orderAdd = (req, res) => {
  let data = req.body

}